<?php

if(!defined('OI')) {
			
	print '<h1>Access Error</h1>Sorry, this file can not be accessed directly, this is strictly forbidden!';
	exit();
				
}

class Home {

	# Store digitalUniversity
	private $digitalUniversity = NULL;

	/*
	 * Constructor: Store instance of sitebuilder
	 * 	- PHP5 now uses pass by reference so we do not need to store the reference
	*/
	public function __construct(DigitalUniversity &$digitalUniversity) {
	
		$this->digitalUniversity =& $digitalUniversity;
				
	}
	
	/*
	 * Destructor: Clean memory parts
	*/
	public function __destruct() {
		
		unset($this->digitalUniversity);
		
	}
	
	private function printBalance() {
		
		echo '<h4>Print Balance</h4>
		<section class="contentBlock">
			<h5>Balance</h5>
			<p>&pound;0.00</p>
			<a class="print_credit" href="#">Top Up</a>
		</section>';

	}
	
	private function universityTwitter() {
	
		echo '<section class="contentBlock">
			<h4>Uni Twitter</h4>
			<div id="twitterFeed">
				<h5>Loading.. please wait</h5>
			</div>
		</section>';
		
	}
	
	/*
	 * loadPage: Detect the current page being viewed and output the relevant
	 * page contents
	*/
	public function outputPage() {
		
		# Query user modules
		$modules = $this->digitalUniversity->db->getResults("SELECT * FROM users_modules um 
		LEFT JOIN modules m ON (m.moduleID = um.moduleID)
		WHERE um.userID = '{$this->digitalUniversity->user['userID']}' AND m.code != ''
		LIMIT 8");
		
		# Get books on loan
		$books = $this->digitalUniversity->db->getResults("SELECT * FROM users_books ub 
		LEFT JOIN books b ON (b.ISBN = ub.ISBN)
		WHERE ub.userID = '{$this->digitalUniversity->user['userID']}'
		LIMIT 4");
		
		# Get news articles
		$news = $this->digitalUniversity->db->getResults("SELECT * FROM news 
		ORDER BY time DESC
		LIMIT 4");
				
		echo '
		<section class="leftColumn">
			<h1>Digital University</h1>
			<nav class="pageTab">
				<a class="focus" href="#" data-tab="home">Home</a>
				<a href="'.$this->digitalUniversity->config['scripturl'].'page=resources" data-tab="notabber">Resources</a>
				<a href="'.$this->digitalUniversity->config['scripturl'].'page=book" data-tab="notabber">Books</a>
				<a href="#" data-tab="news">News &amp; Events</a>
			</nav>
			
			<section id="tab_home" class="dataTab">
				<section class="contentBlock clearfix">
					<h2>Your Modules</h2>';
					
					# Do we have any modules?
					if(!empty($modules)) {
					
						# Loop through modules
						foreach($modules as $k => $v) {
												
							echo '
							<a href="'.$this->digitalUniversity->config['scripturl'].'page=modules&module='.$v['code'].'">
								<article class="document module">
									<h5 class="title">'.$v['code'].' - '.$v['title'].'</h5>
								</article>
							</a>';
							
						}
						
					} else {
					
						echo '<p>You are currently not taking any modules. Please use the button on the right to view all available modules.</p>';	
						
					}
					
					# If we had >= 4, show a link
					echo '<a href="'.$this->digitalUniversity->config['scripturl'].'page=modules" class="viewMore">View More</a>';
								
				echo '</section>
				
				<section class="contentBlock clearfix">
					<h2>Books On Loan</h2>';
					
					# Are we lending anything?
					if(!empty($books)) {
					
						foreach($books as $k => $v) {
						
							echo '
							<a href="'.$this->digitalUniversity->config['scripturl'].'page=book&book='.$v['ISBN'].'">
								<article class="document book">
									<h5 class="title">'.$v['title'].'</h5>
								</article>
							</a>
							<article class="document bookLoan">
								<h3>Date Borrowed</h3>
								<p>'.$this->digitalUniversity->dateTimer('loan', $v['withdrawTime']).'</p>
								<h3>Return Date</h3>
								<p'.(($v['returnTime'] < time()) ? ' style="color: red;"' : '').'>'.$this->digitalUniversity->dateTimer('loan', $v['returnTime']).'</p>  
								<div class="actionLinks">
									<a class="renew" href="'.$this->digitalUniversity->config['scripturl'].'page=book&book='.$v['ISBN'].'&act=renew">Renew</a>
									<a class="rate" href="'.$this->digitalUniversity->config['scripturl'].'page=book&book='.$v['ISBN'].'&act=rate">Rate</a>
								</div>					
							</article>';
							
						}
						
					} else {
						
						echo '<p>You currently have no books on loan. Please use the button on the right to view all available books.</p>';	
						
					}
					
					# More books
					echo '<a href="'.$this->digitalUniversity->config['scripturl'].'page=book" class="viewMore">View More</a>';					
									 
				echo '</section>
			</section>
			
			<section id="tab_news" class="dataTab" style="display: none;">
				<section class="contentBlock">
					<h2>latest news</h2>';
					
					if(!empty($news)) {					
						foreach($news as $k => $v) {	
							
							$v['article'] = strip_tags($v['article'], 'pbi');
							
							if(strlen($v['article']) >= 285) {
								$v['article'] = substr($v['article'], 0, 282).'..';
							}
											
							echo '<article class="newsArticle">
								<h1>'.html_entity_decode($v['title'], ENT_COMPAT, 'UTF-8').'</h1>
								<p>'.$v['article'].'</p>
							</article>';
														
						}						
					}
										
					echo '<a class="viewMore">View More</a>
				</section>
			</section>
									
		</section>
		
		<section class="rightColumn">';
		
		# Print balance
		$this->printBalance();
		$this->universityTwitter();
		
		echo '</section>';
										
	}
		
}

?>